package data.goods;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Locale;

import data.ListInterIterator;

public class Stockfind {
	private ArrayList<Goods> list;
	private ArrayList<GoodsType> templist=new ArrayList<GoodsType>();
	private ArrayList<Goods> find=new ArrayList<Goods>();
	private ArrayList<GoodsType> tempfind=new ArrayList<GoodsType>();
	
	public Stockfind (ListInterIterator<Goods> list){
		this.list=list.toArray();
	}
	
	public ListInterIterator<Goods> find(String key,String message){
		if(key.equals("date")){
			findbydate(message);
		}
		else {
			change();
			GoodsTypeList one=new GoodsTypeList();
			tempfind=one.findByKey(key, message).toArray();
			changeTo();
		}
		return new ListInterIterator<Goods>(find);
	}
	
	private void findbydate(String message){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd",Locale.SIMPLIFIED_CHINESE);
		for(int i=0;i<list.size();i++){
			try {
				if(sdf.parse(list.get(i).getProductionDate()).before(sdf.parse(message))&&
						sdf.parse(list.get(i).getScrapDate()).after(sdf.parse(message))){
					find.add(list.get(i));
				}
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
    private void change(){
    	for(int i=0;i<list.size();i++){
    		templist.add((GoodsType) (new GoodsTypeList()).idFind(list.get(i).getID()));
    	}	
    }
    
    private void changeTo(){
    	for(int i=0;i<list.size();i++){
    		find.add((Goods) (new StockList()).idFind(tempfind.get(i).getID()));
    	}	
    }
}
